<?php

namespace think\wework\Api;

use think\wework\Abstracts\WeworkApiWithToken;

class Tag extends WeworkApiWithToken
{
    private static ?Tag $instance = null;
    public static function getInstance(): Tag
    {
        if(!self::$instance instanceof Tag) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    /**
     * 创建标签
     *
     * @param string $name
     * @param int $id
     * @return array
     */
    public function create(string $name, int $id = 0): array
    {
        $json = ['tagname' => $name];

        if ($id > 0) {
            $json['tagid'] = $id;
        }

        return $this->httpClient->post('tag/create', compact('json'))->toArray();
    }

    /**
     * 更新标签名字
     *
     * @param int $id
     * @param string $name
     * @return array
     */
    public function update(int $id, string $name): array
    {
        $json = ['tagid' => $id, 'tagname' => $name];
        return $this->httpClient->postJson('tag/update', compact('json'))->toArray();
    }

    /**
     * 删除标签
     *
     * @param int $id
     * @return array
     */
    public function delete(int $id): array
    {
        $query = ['tagid' => $id];
        return $this->httpClient->get('tag/delete', compact('query'))->toArray();
    }

    /**
     * 获取标签成员
     *
     * @param int $id
     * @return array
     */
    public function get(int $id): array
    {
        $query = ['tagid' => $id];
        return $this->httpClient->get('tag/get', compact('query'))->toArray();
    }

    /**
     * 增加标签成员
     *
     * @param array $json
     * @return array
     */
    public function addUsers(array $json): array
    {
        return $this->httpClient->post('tag/addtagusers', compact('json'))->toArray();
    }

    /**
     * 删除标签成员
     *
     * @param array $json
     * @return array
     */
    public function delUsers(array $json): array
    {
        return $this->httpClient->post('tag/deltagusers', compact('json'))->toArray();
    }

    /**
     * 获取标签列表
     *
     * @return array
     */
    public function list(): array
    {
        return $this->httpClient->get('tag/list')->toArray();
    }
}
